package com.phone.store.service.impl;

import com.phone.store.dto.OrderDTO;
import com.phone.store.entity.OrderMaster;
import com.phone.store.entity.PhoneInfo;
import com.phone.store.entity.PhoneSpecs;
import com.phone.store.enums.PayStatusEnum;
import com.phone.store.enums.ResultEnum;
import com.phone.store.exception.PhoneException;
import com.phone.store.repository.OrderMasterRepository;
import com.phone.store.repository.PhoneInfoRepository;
import com.phone.store.repository.PhoneSpecsRepository;
import com.phone.store.service.OrderService;
import com.phone.store.service.PhoneService;
import com.phone.store.util.KeyUtil;
import com.phone.store.vo.OrderDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/8/1 10:43 上午
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private PhoneSpecsRepository phoneSpecsRepository;
    @Autowired
    private PhoneInfoRepository phoneInfoRepository;
    @Autowired
    private OrderMasterRepository orderMasterRepository;
    @Autowired
    private PhoneService phoneService;

    @Override
    public OrderDTO createOrder(OrderDTO orderDTO) {
        OrderMaster orderMaster = new OrderMaster();
        BeanUtils.copyProperties(orderDTO, orderMaster);

        PhoneSpecs phoneSpecs = phoneSpecsRepository.findById(orderDTO.getSpecsId()).get();
        if (phoneSpecs == null) {
            log.error("【创建订单】规格不存在, phoneSpecs={}", phoneSpecs);
            throw new PhoneException(ResultEnum.SPECS_NOT_EXIST);
        }
        BeanUtils.copyProperties(phoneSpecs, orderMaster);

        PhoneInfo phoneInfo = phoneInfoRepository.findById(phoneSpecs.getPhoneId()).get();
        if (phoneInfo == null) {
            log.error("【创建订单】商品不存在, phoneSpecs={}", phoneInfo);
            throw new PhoneException(ResultEnum.PHONE_NOT_EXIST);
        }
        BeanUtils.copyProperties(phoneInfo, orderMaster);

        // 计算总价
        BigDecimal orderAmount = new BigDecimal(0);
        // 获取规格价格/100*规格
        orderAmount = phoneSpecs.getSpecsPrice().divide(new BigDecimal(100))
                .multiply(new BigDecimal(orderDTO.getPhoneQuantity()))
                .add(orderAmount)
                .add(new BigDecimal(10));
        orderMaster.setOrderAmount(orderAmount);

        // orderId
        orderMaster.setOrderId(KeyUtil.createUniqueKey());
        orderDTO.setOrderId(orderMaster.getOrderId());

        // payStatus
        orderMaster.setPayStatus(PayStatusEnum.UNPAID.getCode());

        orderMasterRepository.save(orderMaster);

        // 库存
        phoneService.subStock(orderDTO.getSpecsId(), orderDTO.getPhoneQuantity());

        return orderDTO;
    }

    @Override
    public OrderDetailVO findOrderDetailByOrderId(String orderId) {
        OrderDetailVO orderDetailVO = new OrderDetailVO();

        OrderMaster orderMaster = orderMasterRepository.findById(orderId).get();
        log.info("[订单信息]=>{}", orderMaster);
        if (orderMaster == null) {
            log.error("【查询订单】订单不存在, orderMaster=>{}", orderMaster);
            throw new PhoneException(ResultEnum.ORDER_NOT_EXIST);
        }

        BeanUtils.copyProperties(orderMaster, orderDetailVO);
        String price = orderMaster.getSpecsPrice().divide(new BigDecimal(100)) + "";
        orderDetailVO.setSpecsPrice(price);

        return orderDetailVO;
    }

    @Override
    public String pay(String orderId) {
        OrderMaster orderMaster = orderMasterRepository.findById(orderId).get();

        if (orderMaster == null) {
            log.error("【订单支付】订单不存在, orderMaster=>{}", orderMaster);
            throw new PhoneException(ResultEnum.ORDER_NOT_EXIST);
        }

        // TODO
        if (orderMaster.getPayStatus().equals(PayStatusEnum.UNPAID.getCode())) {
            orderMaster.setPayStatus(PayStatusEnum.PAID.getCode());
            orderMasterRepository.save(orderMaster);
        } else {
            log.error("【支付订单】订单已支付, orderMaster=>{}", orderMaster);
        }

        return orderId;
    }
}
